package com.adee.design.create.builder;

// 建造者模式：使用一个包装类对原类进行包装，在不改变原类的情况下，在不同的包装类中实现不同的构造逻辑。
// 指挥者
public class Test {
    public static void main(String[] args) {
        ProductBuilder<Product> pb = new DefaultProductBuilder<Product>()
                .setProductName("五系")
                .setCompanyName("BMW")
                .setPart1("发动机")
                .setPart2("底盘")
                .setPart3("变速箱")
                .setPart4("中控台");
        Product p = pb.build();

        ProductBuilder pb1 = new FacadeProductBuilder("5系", "BMW", "发动机", "底盘", "变速箱", "中控台");
        pb1.build();
        System.out.println();
    }
}

class Product {
    String productName;
    String companyName;
    String part1;
    String part2;
    String part3;
    String part4;
}
// 抽象建造者
interface ProductBuilder<T> {
    ProductBuilder setProductName(String productName);
    ProductBuilder setCompanyName(String companyName);
    ProductBuilder setPart1(String part1);
    ProductBuilder setPart2(String part2);
    ProductBuilder setPart3(String part3);
    ProductBuilder setPart4(String part4);

    T build();
}

// 默认具体建造者
class DefaultProductBuilder<T> implements ProductBuilder<T> {
    protected Product product = new Product();
    public DefaultProductBuilder() {}
    public DefaultProductBuilder(String productName, String companyName, String part1, String part2, String part3, String part4) {
        this.product.productName = productName;
        this.product.companyName = companyName;
        this.product.part1 = part1;
        this.product.part2 = part2;
        this.product.part3 = part3;
        this.product.part4 = part4;
    }

    @Override
    public ProductBuilder setProductName(String productName) {
        this.product.productName = productName;
        return this;
    }

    @Override
    public ProductBuilder setCompanyName(String companyName) {
        this.product.companyName = companyName;
        return this;
    }

    @Override
    public ProductBuilder setPart1(String part1) {
        this.product.part1 = part1;
        return this;
    }

    @Override
    public ProductBuilder setPart2(String part2) {
        this.product.part2 = part2;
        return this;
    }

    @Override
    public ProductBuilder setPart3(String part3) {
        this.product.part3 = part3;
        return this;
    }

    @Override
    public ProductBuilder setPart4(String part4) {
        this.product.part4 = part4;
        return this;
    }

    @Override
    public T build() { // 执行构造逻辑
        System.out.println("productName:" + this.product.productName);
        System.out.println("companyName:" + this.product.companyName);
        System.out.println("part1:" + this.product.part1);
        System.out.println("part2:" + this.product.part2);
        System.out.println("part3:" + this.product.part3);
        System.out.println("part4:" + this.product.part4);
        return (T)this.product;
    }
}

// 另一个建造者，在默认建造者基础上进行逻辑加强
class FacadeProductBuilder extends DefaultProductBuilder {

    public FacadeProductBuilder(String productName, String companyName, String part1, String part2, String part3, String part4) {
        super(productName, companyName, part1, part2, part3, part4);
    }

    @Override
    public Product build() { // 执行构造逻辑
        super.build();
        System.out.println("对各个part进行外观美化和关联调试");
        return this.product;
    }
}

